package com.example.springboot.controller;

import com.example.springboot.pojo.Result;
import com.example.springboot.pojo.Token;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.JwtUtil;
import com.example.springboot.utils.Md5Util;
import com.example.springboot.utils.ThreadLocalUtil;
import com.github.pagehelper.PageInfo;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.pojo.User;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private Token token;
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password, @Pattern(regexp = "^\\S{11}$") String userphone, @Pattern(regexp = "^\\S{18}$") String idCard) {
        //查询用户
        User u = userService.findByUserName(username);
        if (u == null) {

            userService.register(username, password, userphone, idCard);
            return Result.success();
        } else {
            return Result.error("该账号已被占用");
        }
    }
    @PostMapping("/login")
    public Result<Token> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        //根据用户名查询用户
        User loginUser = userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名错误");
        }
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            //登录成功
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            token.setToken(JwtUtil.genToken(claims));
            token.setRole(loginUser.getRole());
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")
    public Result<User> userInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/findByusername")
    public Result<User> findByusername(@RequestParam String username) {
        User user = userService.findByUserName(username);
        return Result.success(user);
    }

    @DeleteMapping("/delete")
    public Result delete(Integer id)
    {
        userService.delete(id);
        return Result.success();
    }
    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> page = userService.selectPage(pageNum, pageSize);
        return Result.success(page);
    }
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatch(ids);
        return Result.success();
    }
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }
}

